import http from './http'

// =====================================================
// 认证 API
// =====================================================

/**
 * 登录
 * @param {Object} data - { username, password }
 * @returns {Promise<Object>} { code, message, data: { token, role, user } }
 */
export const login = (data) => {
  return http.post('/v1/auth/login', data)
}

/**
 * 获取当前用户信息
 * @returns {Promise<Object>} { code, message, data: { user, role } }
 */
export const getCurrentUser = () => {
  return http.get('/v1/auth/me')
}

// =====================================================
// 学生端 API
// =====================================================

/**
 * 获取学生的请假列表
 * @param {Object} params - { status?, type?, page?, pageSize?, sortBy?, sortOrder? }
 * @returns {Promise<Object>}
 */
export const getStudentLeaves = (params) => {
  return http.get('/v1/student/leaves', { params })
}

/**
 * 创建请假申请
 * @param {Object} data - { type, startAt, endAt, reason }
 * @returns {Promise<Object>}
 */
export const createLeave = (data) => {
  return http.post('/v1/student/leaves', data)
}

/**
 * 获取请假申请详情（学生）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const getStudentLeaveDetail = (id) => {
  return http.get(`/v1/student/leaves/${id}`)
}

/**
 * 撤销请假申请（学生）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const cancelStudentLeave = (id) => {
  return http.delete(`/v1/student/leaves/${id}`)
}

// =====================================================
// 老师端 API
// =====================================================

/**
 * 获取待审批列表
 * @param {Object} params - { page?, pageSize? }
 * @returns {Promise<Object>}
 */
export const getApprovals = (params) => {
  return http.get('/v1/teacher/approvals', { params })
}

/**
 * 获取请假申请详情（老师）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const getTeacherLeaveDetail = (id) => {
  return http.get(`/v1/teacher/approvals/${id}`)
}

/**
 * 审批请假申请（老师统一接口）
 * @param {number} id
 * @param {Object} data - { approved: boolean, comment: string }
 * @returns {Promise<Object>}
 */
export const approveLeave = (id, data) => {
  return http.post(`/v1/teacher/approvals/${id}`, data)
}

// =====================================================
// 管理员 API
// =====================================================

/**
 * 获取所有请假申请列表（管理员）
 * @param {Object} params - { status?, type?, page?, pageSize?, sortBy?, sortOrder? }
 * @returns {Promise<Object>}
 */
export const getAdminLeaves = (params) => {
  return http.get('/v1/admin/leaves', { params })
}

/**
 * 获取请假申请详情（管理员）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const getAdminLeaveDetail = (id) => {
  return http.get(`/v1/admin/leaves/${id}`)
}

/**
 * 撤销请假申请（管理员）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const cancelAdminLeave = (id) => {
  return http.post(`/v1/admin/leaves/${id}/cancel`)
}

/**
 * 标记请假申请为无效（管理员）
 * @param {number} id
 * @returns {Promise<Object>}
 */
export const markLeaveInvalid = (id) => {
  return http.post(`/v1/admin/leaves/${id}/mark-invalid`)
}

/**
 * 获取统计数据
 * @param {Object} params - { start?, end? }
 * @returns {Promise<Object>}
 */
export const getStats = (params) => {
  return http.get('/v1/admin/stats', { params })
}
